package com.smartstate.IOC.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.smartstate.IOC.entity.HalfHourPowerMeterSummary;
import com.smartstate.IOC.vo.PowerMeterSummaryRec;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface HalfHourPowerMeterSummaryMapper extends BaseMapper<HalfHourPowerMeterSummary> {

    @Select(" select sum(IFNULL(detActiveEnergy,0) + IFNULL(detReactiveEnergy,0) + IFNULL(detApparentEnergy,0)) summary_energy, dayStr day_period from \n" +
            "half_hour_power_meter_summary where dayStr > #{startTime} and dayStr <= #{endTime} GROUP BY dayStr order by dayStr  ")
    List<PowerMeterSummaryRec> getPowerMeterSummaryRecs(@Param("startTime")String startTime, @Param("endTime")String endTime);


    @Select(" select sumRec.meterId device_id, sumRec.day_period day_period, sumRec.summary_energy summary_energy,eqp.`name` device_name from (\n" +
            "select meterId, dayStr day_period, sum(IFNULL(detActiveEnergy,0) + IFNULL(detReactiveEnergy,0) + IFNULL(detApparentEnergy,0)) summary_energy  \n" +
            "from  half_hour_power_meter_summary \n" +
            "where  dayStr > #{startTime}  and  dayStr <= #{endTime} \n" +
            "GROUP BY dayStr, meterId ) sumRec\n" +
            "INNER JOIN equipment eqp on sumRec.meterId = eqp.deviceId \n" +
            "order by sumRec.day_period  ")
    List<PowerMeterSummaryRec> getPeriodDeviceSummaryRecs(@Param("startTime")String startTime, @Param("endTime")String endTime);



    @Select("<script> select objId, dayStr day_period, sum(IFNULL(detActiveEnergy,0) + IFNULL(detReactiveEnergy,0) + IFNULL(detApparentEnergy,0)) summary_energy\n" +
            "        from  half_hour_power_meter_summary\n" +
            "        where 1=1\n" +
            "        <if test=\"moudelIds != null  and  moudelIds.size() > 0 \">\n" +
            "            and objId  in\n" +
            "            <foreach collection=\"moudelIds\"  item=\"item\" open=\"(\" close=\")\"  separator=\",\" index=\"index\" >\n" +
            "                #{item}\n" +
            "            </foreach>\n" +
            "        </if>\n" +" <if test=\" startTime !=null and startTime!='' \">\n" +
            "            and dayStr &gt; #{startTime}\n" +
            "        </if>\n" +
            "        <if test=\" endTime !=null and endTime!='' \">\n" +
            "            and  dayStr &lt;= #{endTime}\n" +
            "        </if> " +
            "        GROUP BY dayStr, objId order by dayStr </script> ")
    List<PowerMeterSummaryRec> getPeriodDeviceSummaryRecsByMoudelIds(@Param("startTime")String startTime, @Param("endTime")String endTime,@Param("moudelIds") List<String> moudelIds);



    @Select("SELECT monthStr from half_hour_power_meter_summary  GROUP BY monthStr")
    List<String> getmonthStr();

    @Select("select SUM(detApparentEnergy) from  half_hour_power_meter_summary where monthStr =  #{string}")
    Float getMouthData(String string);

    @Select("SELECT count(monthStr) from half_hour_power_meter_summary where monthStr = #{s}")
    int getNoMouth(String s);
}
